package com.zb.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.zb.entity.Car;
import com.zb.entity.Mr;
import com.zb.entity.Msi;
import com.zb.entity.Parts;
import com.zb.entity.Store;
import com.zb.entity.Wx;
import com.zb.entity.Yh;
import com.zb.service.CarService;
import com.zb.service.MrService;
import com.zb.service.MsiService;
import com.zb.service.PartsService;
import com.zb.service.StoreService;
import com.zb.service.WxService;
import com.zb.service.YhService;
import com.zb.service.impl.CarServiceImpl;
import com.zb.service.impl.MrServiceImpl;
import com.zb.service.impl.MsiServiceImpl;
import com.zb.service.impl.PartsServiceImpl;
import com.zb.service.impl.StoreServiceImpl;
import com.zb.service.impl.WxServiceImpl;
import com.zb.service.impl.YhServiceImpl;
import com.zb.util.PageUtil;

public class PartsServlet extends HttpServlet {
	private PartsService part=new PartsServiceImpl();
	private MsiService msi=new MsiServiceImpl();
	private StoreService stor=new StoreServiceImpl();
	private CarService car=new CarServiceImpl();
	private MrService mr=new MrServiceImpl();
	private WxService wx=new WxServiceImpl();
	private YhService yh=new YhServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		String op=request.getParameter("op");
		if(op.equals("Parts")) {
			this.Parts(request, response);
		}else if(op.equals("mrby")) {
			this.store(request, response);
		}else if(op.equals("addmr")) {
			this.addmr(request, response);
		}else if(op.equals("wxby")){
			this.wxby(request, response);
		}else if(op.equals("addwx")) {
			this.addwx(request, response);
		}else if(op.equals("yhby")) {
			this.yhby(request, response);
		}else if(op.equals("addyh")) {
			this.addyh(request, response);
		}
	}
	protected void Parts(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		int index=1;
		int size=3;
		String name=null;
		int msi=0;
		if(request.getParameter("index")!=null&&!"".equals(request.getParameter("index"))){
			index=Integer.parseInt(request.getParameter("index"));
		}
		if(request.getParameter("msi")!=null&&!"".equals(request.getParameter("msi"))){
			msi=Integer.parseInt(request.getParameter("msi"));
		}
		PageUtil<Parts>page=part.getPartsPage(index, size, name, msi);

		request.setAttribute("page",page);
		if("fenye".equals(request.getParameter("fenye"))) {
			String str=JSON.toJSONString(page);
			PrintWriter out=response.getWriter();
			out.print(str);
			out.flush();
			out.close();
		}else {
			request.getRequestDispatcher("store.jsp").forward(request, response);
		}
		
	}
	
	protected void store(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Msi>m=msi.getAllList();
		List<Store> store=stor.getAllStore();
		request.setAttribute("m",m);
		request.setAttribute("s",store);
		request.getRequestDispatcher("mrby.jsp").forward(request, response);
	}
	protected void addmr(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String carNum=request.getParameter("carNum");
		String carName=request.getParameter("carName");
		String carAir=request.getParameter("carAir");
		Car c=new Car();
		c.setCarAir(carAir);
		c.setCarName(carName);
		c.setCarNum(carNum);
		car.addCar(c);
		
		int max=car.getMax();
		String featExp=request.getParameter("featExp");
		int msi=Integer.parseInt(request.getParameter("msi"));
		int store=Integer.parseInt(request.getParameter("store"));
		String time=request.getParameter("time");
		Mr m=new Mr();
		m.setCar(max);;
		m.setFeatDate(time);
		m.setFeatExp(featExp);
		Msi ms=new Msi();
		ms.setId(msi);
		m.setMsi(ms);
		Store st=new Store();
		st.setId(store);
		m.setStore(st);
		mr.addMr(m);
	}
	protected void wxby(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Msi>m=msi.getAllList();
		List<Store> store=stor.getAllStore();
		request.setAttribute("m",m);
		request.setAttribute("s",store);
		request.getRequestDispatcher("wxby.jsp").forward(request, response);
	}
	protected void addwx(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String carNum=request.getParameter("carNum");
		String carName=request.getParameter("carName");
		String carAir=request.getParameter("carAir");
		Car c=new Car();
		c.setCarAir(carAir);
		c.setCarName(carName);
		c.setCarNum(carNum);
		car.addCar(c);
		
		int max=car.getMax();
		String featExp=request.getParameter("featExp");
		int msi=Integer.parseInt(request.getParameter("msi"));
		int store=Integer.parseInt(request.getParameter("store"));
		String time=request.getParameter("time");
		
		Wx w=new Wx();
		w.setCar(max);
		w.setServiceExp(featExp);
		Msi ms=new Msi();
		ms.setId(msi);
		w.setMsi(ms);
		Store st=new Store();
		st.setId(store);
		w.setStore(st);
		wx.addWx(w);
		
	}
	protected void yhby(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Msi>m=msi.getAllList();
		List<Store> store=stor.getAllStore();
		request.setAttribute("m",m);
		request.setAttribute("s",store);
		request.getRequestDispatcher("yhby.jsp").forward(request, response);
	}
	protected void addyh(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String carNum=request.getParameter("carNum");
		String carName=request.getParameter("carName");
		String carAir=request.getParameter("carAir");
		Car c=new Car();
		c.setCarAir(carAir);
		c.setCarName(carName);
		c.setCarNum(carNum);
		car.addCar(c);
		
		int max=car.getMax();
		String featExp=request.getParameter("featExp");
		int msi=Integer.parseInt(request.getParameter("msi"));
		int store=Integer.parseInt(request.getParameter("store"));
		String time=request.getParameter("time");
		Yh y=new Yh();
		y.setCar(max);
		y.setStickExp(featExp);
		Msi ms=new Msi();
		ms.setId(msi);
		y.setMsi(ms);
		Store st=new Store();
		st.setId(store);
		y.setStore(st);
		yh.addYh(y);
	}
}
